package com.his.system.manager.base;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @Description
 * @Author Admin
 * @Date 2020-06-14 17:55:34
 */
@Slf4j
public class BaseResult<T extends Object> implements Serializable {

    /**
     * 返回是否成功
     */
    private Boolean success = false;

    /**
     * 返回信息
     */
    private String message = "操作成功";

    /**
     * 返回消息类型,用于复杂返回信息,0代表普通返回信息,1代表特殊返回信息
     */
    private Integer code = 0;

    private T content;

    public BaseResult() {
    }

    private BaseResult(Boolean success, String message, Integer code, T content) {
        this.success = success;
        this.message = message;
        this.code = code;
        this.content = content;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setMsg(String msg) {
        this.message = msg;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public Integer getCode() {
        return code;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    /**
     * 构建返回结果
     *
     * @param success
     * @param message
     * @param code
     * @param data
     * @return
     */
    public static BaseResult build(Boolean success, String message, Integer code, Object data) {

        return new BaseResult(success, message, code, data);
    }

    /**
     * 构建返回结果，code默认值为0
     *
     * @param success
     * @param msg
     * @param data
     * @return
     */
    public static BaseResult build(Boolean success, String msg, Object data) {
        return build(success, msg, 0, data);
    }

    /**
     * 构建成功结果
     *
     * @param message
     * @param data
     * @return
     */
    public static BaseResult buildSuccess(String message, Object data) {
        return build(Boolean.TRUE, message, ResponseCode.SUCCESS.getCode(), data);
    }

    /**
     * 胡焕金20200103已经将其弃用,请使用buildSuccessMsg(String message)
     * 因为有可能导致误用,而且编译器发现不了这种问题(比如我想返回一个data="xxx"的BaseResult.调用该方法的结果却是data=new JSONObject();这并不是想要的结果)
     * <p>
     * 构建成功结果带信息
     *
     * @param message
     * @return
     */
    @Deprecated
    public static BaseResult buildSuccess(String message) {
        return buildSuccess(message, new JSONObject());
    }

    /**
     * 构建成功结果带信息(建议使用)
     *
     * @param message
     * @return
     */
    public static BaseResult buildSuccessMsg(String message) {
        return buildSuccess(message, new JSONObject());
    }

    /**
     * 构建成功结果待数据
     *
     * @param data
     * @return
     */
    public static BaseResult buildSuccess(Object data) {
        return buildSuccess(ResponseCode.SUCCESS.getMessage(), data);
    }

    /**
     * 构建失败结果
     *
     * @param message
     * @param data
     * @return
     */
    public static BaseResult buildFailure(Integer code, String message, Object data) {
        return build(Boolean.FALSE, message, code, data);
    }

    /**
     * 构建失败结果
     *
     * @param message
     * @param data
     * @return
     */
    public static BaseResult buildFailure(String message, Object data) {
        return build(Boolean.FALSE, message, ResponseCode.ERROR.getCode(), data);
    }

    /**
     * 构建失败结果待数据
     *
     * @param message
     * @return
     */
    public static BaseResult buildFailure(String message) {
        return buildFailure(message, new JSONObject());
    }

    /**
     * 构建失败结果待数据
     *
     * @param code
     * @param message
     * @return
     */
    public static BaseResult buildFailure(Integer code, String message) {
        return build(Boolean.FALSE, message, code, new JSONObject());
    }

    /**
     * 构建失败结果带数据
     *
     * @param data
     * @return
     */
    public static BaseResult buildFailure(Object data) {
        return buildFailure("操作失败", data);
    }

    /**
     * 构建失败结果
     *
     * @return
     */
    public static BaseResult buildFailure() {
        return buildFailure("操作失败", new JSONObject());
    }

    /**
     * 构建错误结果
     *
     * @return
     */
    public static BaseResult buildError() {
        return build(Boolean.FALSE, ResponseCode.ERROR.getMessage(), ResponseCode.ERROR.getCode(), new JSONObject());
    }

    /**
     * 构建Hystrix失败结果
     *
     * @return
     */
    public static BaseResult buildHystrixFailure() {
        return buildFailure(ResponseCode.HYSTRIX_FAST_FAIL.getCode(), ResponseCode.HYSTRIX_FAST_FAIL.getMessage(), new JSONObject());
    }

    /**
     * 构建无效入参失败结果
     *
     * @return
     */
    public static BaseResult buildInvalidFailure() {
        return buildFailure(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.INVALID_PARAM.getMessage(), new JSONObject());
    }

    /**
     * 构建数据不存在失败结果
     *
     * @return
     */
    public static BaseResult buildDataNotFoundFailure() {
        return buildFailure(ResponseCode.DATA_NOT_FOUND.getCode(), ResponseCode.DATA_NOT_FOUND.getMessage(), new JSONObject());
    }
}
